package framework.movements;

import org.newdawn.slick.GameContainer;

import framework.Updatable;

public class Movement implements Updatable{
	
	private int duration;
	
	protected int currentTime;
	
	protected int blockingTime;
	
	private int x;
	
	private int y;
	
	private MovementStrategy mode;
	
	private int stayTimeEnd;
	
	public Movement() {
		mode = null;
		blockingTime = getDuration();
	}

	public MovementStrategy getMode() {
		return mode;
	}

	public void setMode(MovementStrategy mode) {
		this.mode = mode;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		if (isActive()) {
			this.currentTime += delta;
			if (this.currentTime < getDuration() - getStayTimeEnd()) {
				if (mode != null) {
				    setX(mode.computeX(delta));
				    setY(mode.computeY(delta));			
				}
			}
			if (this.currentTime >= getDuration()) {
				interrupt();
			}
		}	
	}
	
	public void trigger() {
		currentTime = 1;
	}
	
	public void interrupt() {
		currentTime = 0;
	}
	
	public boolean isActive() {
		return currentTime > 0 && currentTime <= getDuration();
	}
	
	protected void setX(int x) {
		this.x = x;
	}
	
	protected void setY(int y) {
		this.y = y;
	}
	
	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}
	
	public int getStayTimeEnd() {
		return stayTimeEnd;
	}

	public void setStayTimeEnd(int stayTimeEnd) {
		this.stayTimeEnd = stayTimeEnd;
	}
	
	public int getDuration() {
		return duration;
	}
	
	public void setDuration(int duration) {
		this.duration = duration;
	}
	
	public int getCurrentTime() {
		return currentTime;
	}
	
	public int getBlockingTime() {
		return currentTime;
	}
	
	public void setBlockingTime(int blockingTime) {
		this.blockingTime = blockingTime;
	}
	
	public boolean isBlocking() {
		return isActive() && currentTime <= blockingTime;
	}
}
